package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    public void clearImg(){
        //根据redis中存储的两个set集合，计算出的差值就是需要清除的垃圾图片
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (sdiff!=null){
            //遍历sdiff集合
            for (String s : sdiff) {
                //调用工具类根据遍历出的图片名删除七牛云中的图片
                QiniuUtils.deleteFileFromQiniu(s);
                //将已经删除了的图片名称从setmealPicResource集合中删除
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
                System.out.println("定时任务执行，清楚垃圾图片："+s);
            }
        }
    }
}
